package com.numberone.master.modules.rsa.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.config.Global;
import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.rsa.entity.Ips;
import com.numberone.master.modules.rsa.service.IpsService;
import com.numberone.master.modules.sys.utils.UserUtils;

/**
 * ip地址Controller
 * @author ck
 * @version 2018-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/rsa/ips")
public class IpsController extends BaseController {

	@Autowired
	private IpsService ipsService;

	 /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
	@ModelAttribute
	public Ips get(@RequestParam(required=false) String id) {
		Ips entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ipsService.get(id);
		}
		if (entity == null){
			entity = new Ips();
		}
		return entity;
	}

	 /**
     * list列表
     * @param ips
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("rsa:ips:list")
	@RequestMapping(value = {"list", ""})
	public String list(Ips ips, HttpServletRequest request, HttpServletResponse response, Model model) {
		ips.setCreateBy(UserUtils.getUser());
		Page<Ips> page = ipsService.findPage(new Page<Ips>(request, response), ips); 
		model.addAttribute("page", page);
		return "modules/rsa/ipsList";
	}

	/**
     * 打开增加页面
     * @return
     */
	@RequiresPermissions("rsa:ips:save")
	@RequestMapping(value = "openSave")
	public String openSave() {
		return "modules/rsa/ipsSave";
	}


	/**
	 * 打开修改页面
	 * @param ips
	 * @param model
	 * @return
	 */
	@RequiresPermissions("rsa:ips:update")
	@RequestMapping(value = "openUpdate")
	public String openUpdate(Ips ips, Model model) {
		model.addAttribute("ips", ips);
		return "modules/rsa/ipsUpdate";
	}

	/**
	 * 添加
	 * @param ips
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("rsa:ips:save")
	@RequestMapping(value = "save")
	public String save(Ips ips, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ips)){
			return openSave();
		}
		ips.setCreateDate(new Date());
		ips.setUpdateBy(UserUtils.getUser());
		ips.setUpdateDate(new Date());
		ips.setCreateBy(UserUtils.getUser());
		ipsService.save(ips);
		addMessage(redirectAttributes, "添加ip成功");
		return "redirect:"+Global.getAdminPath()+"/rsa/ips/list?repage";
	}

	/**
	 * 修改
	 * @param ips
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("rsa:ips:update")
	@RequestMapping(value = "update")
	public String update(Ips ips, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ips)){
			return openUpdate(ips, model);
		}
		ipsService.update(ips);
		addMessage(redirectAttributes, "修改ip成功");
		return "redirect:"+Global.getAdminPath()+"/rsa/ips/?repage";
	}

	 /**
     * 删除
     * @param ips
     * @param redirectAttributes
     * @return
     */
	@RequiresPermissions("rsa:ips:delete")
	@RequestMapping(value = "delete")
	public String delete(Ips ips, RedirectAttributes redirectAttributes) {
		ipsService.delete(ips);
		addMessage(redirectAttributes, "删除ip成功");
		return "redirect:"+Global.getAdminPath()+"/rsa/ips/?repage";
	}
	
	/**
	 * 根据ip查询是否重复
	 * @param oldIp
	 * @param ip
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "isIpsByIp")
	public String isIpsByIp(String oldIp, String ip) {
		if (ip !=null && ip.equals(oldIp)) {
			return "true";
		} else if (ip !=null && ipsService.isIps(ip) == null) {
			return "true";
		}
		return "false";
	}

}